package operator;


import org.apache.spark.SparkConf;
import org.apache.spark.api.java.JavaRDD;
import org.apache.spark.api.java.JavaSparkContext;
import org.apache.spark.api.java.function.FlatMapFunction;
import org.apache.spark.api.java.function.VoidFunction;
import java.util.Arrays;
import java.util.List;

/**
 * Created by huangxiaoli on 2019/11/27 14:03
 */
public class flatMap {

    public static void main(String[] args) {
        SparkConf conf = new SparkConf()
                .setAppName("flatMap")
                .setMaster("local");

        JavaSparkContext sc = new JavaSparkContext(conf);

        List<String> lineList = Arrays.asList("hello you","hello me","hello world");

        JavaRDD<String> lines = sc.parallelize(lineList);

        //对RDD执行flatMap算子，将每一行文本，拆分为多个单词
        //在这里，传入第一行，hello,you
        //返回的是一个Iterable<String>(hello,you)
        JavaRDD<String> words = lines.flatMap((FlatMapFunction<String, String>) t -> Arrays.asList(t.split(" ")).iterator());

        words.foreach(new VoidFunction<String>() {
            @Override
            public void call(String t) throws Exception {
                System.out.println(t);
            }
        });
        sc.close();
    }

}
